package com.xsm.activitygroup;

import com.umeng.analytics.MobclickAgent;
import com.xsm.activitygroup.R;
import com.xsm.adapter.TabAdapter;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;
import android.app.ActivityGroup;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
public class MainActivity extends ActivityGroup {
	private	ViewGroup contents;
	private	GridView tabs;
	private	Context context;
	private boolean isSelected[];
	private   LoginOutBroadCast broadCast;
	private String FLAG="LOGINOUTUSER";
	protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        super.onCreate(savedInstanceState);
        context=this;
        initview();
        broadCast=new LoginOutBroadCast();
       IntentFilter filter=new IntentFilter();
       filter.addAction(FLAG);
       registerReceiver(broadCast, filter);
    }  
    //初始化Tab
    public void initview(){
    	String texts[]=new String[]{"推荐","市场","发布","设置"};
    	int icons[]=new int[]{R.drawable.tuijian,R.drawable.shop,R.drawable.fabu,R.drawable.set};
    	int selecticons[]=new int[]{R.drawable.tuijianpre,R.drawable.shoppre,R.drawable.fabupre,R.drawable.setpre};
    	contents=(ViewGroup)findViewById(R.id.contents);
    	Tabchange(1);
    	tabs=(GridView)findViewById(R.id.tabs);
    	final TabAdapter adapter=new TabAdapter(context,texts,icons,selecticons);
    	adapter.setSelected(0,true);
    	tabs.setAdapter(adapter);
	
    	tabs.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
		    isSelected=adapter.getSelected();		
			Tabchange(arg2+1);
			for(int i=0;i<isSelected.length;i++){
				if(i==arg2)
			adapter.setSelected(i,true);
				else 
			adapter.setSelected(i,false);
			}
		adapter.notifyDataSetChanged();	
			}
    	});   	
    }
    //转换页面
	public void Tabchange(int index){
		Class c = null;
    	switch (index) {
		case 1:
		  c=activity1.class;	
			break;
		case 2:
			c=activity2.class;	
		break;
		case 3:
		c=activity3.class;
		break;
		case 4:
		c=activity4.class;	
	    break;
		default:
	    break;
		}
    	contents.removeAllViews();
		View view=getLocalActivityManager().startActivity("acticity"+index,new Intent(context,c)).getDecorView();
		LayoutParams params=new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
		view.setLayoutParams(params);
    	contents.addView(view);	
  
    }
	
	 public boolean onKeyDown(int keyCode, KeyEvent event) {
         return getCurrentActivity().onKeyDown(keyCode, event);
 }
	
	 protected void onResume() {
	 	MobclickAgent.onResume(this);
	 	super.onResume();
	 }
	 protected void onPause() {
	 	MobclickAgent.onResume(this);
	 	super.onPause();
	 }
	 @Override
	protected void onDestroy() {
	unregisterReceiver(broadCast);
		super.onDestroy();
	}
	 class LoginOutBroadCast extends BroadcastReceiver{
		public void onReceive(Context context, Intent intent) {
			 String message = intent.getStringExtra("message");
			if(message.equals("LoginOut")){
				finish();
			}
		}
	 }
}

